package com.gmall.web.controller;

import com.gmall.common.result.Result;
import com.gmall.item.feign.ItemApiFeignClient;
import com.gmall.web.utils.FreemarkerUtil;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class ItemContoller {

    @Autowired
    ItemApiFeignClient itemApiFeignClient;
    @GetMapping("item_ftl_{skuId}")
    public String item(@PathVariable String skuId, Model model){

        /**
         * spring:
         *   freemarker:
         *     template-loader-path: classpath:/templates/
         *     suffix: .ftl
         */
        Result<Map<String, Object>> item
                = itemApiFeignClient.getItem(Long.valueOf(skuId));
        /**
         * resultMap
         * key                value
         * skuInfo          Sku信息
         * categoryView     一级，二级，三级分类
         * skuImageList     Sku的图片列表
         * tm              品牌信息
         */
        model.addAllAttributes(item.getData());
        System.out.println("skuId= "+ skuId);
        return "item";
    }

    private final String templateName = "item";
    @GetMapping("gen_item_{skuId}")
    @ResponseBody
    public Result<String> gen_item(@PathVariable String skuId) throws TemplateException, IOException {

        FreemarkerUtil freemarkerUtil = new FreemarkerUtil();

        Result<Map<String, Object>> item
                = itemApiFeignClient.getItem(Long.valueOf(skuId));
        HashMap<String, Object> data = (HashMap)item.getData();
        /**
         * resultMap
         * key                value
         * skuInfo          Sku信息
         * categoryView     一级，二级，三级分类
         * skuImageList     Sku的图片列表
         * tm              品牌信息
         */

        //String htmlFilePath = "d:\\01_2109A\\gmall-parent\\web\\target\\classes\\static\\item_" + skuId + ".html";
        String htmlFilePath = "d:\\01_2109A\\software\\nginx-1.22.0\\gmall\\item_" + skuId + ".html";
        freemarkerUtil.createHtml(data,"item",htmlFilePath);
        /*//1.创建配置类
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
        //2.设置模板所在的目录
        // 获取resources路径
        String classpath = this.getClass().getResource("/").getPath();
        // 配置模板路径
        configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate(templateName+".ftl");
        //5.创建数据模型
        Map map=new HashMap();
        map.put("skuName", "华为 64G玫瑰金色 移动通信电信4G手机 ");
        //6.创建Writer对象
        String outputFilePath = "d:\\01_2109A\\gmall-parent\\web\\target\\classes\\static\\item_" + skuId + ".html";
        Writer out =new FileWriter(new File(outputFilePath));
        //7.输出
        template.process(map, out);
        //8.关闭Writer对象
        out.close();*/
        return Result.ok("success");
    }
}
